<template>
	<view class="user">
		<view class="list">
			<view class="user-item" v-for="(item, index) in dataObj.list" :key="index">
				<view class="head">
					<image class="head-img" :src="item.avatar"></image>
					<view class="item">
						<view class="item-date">{{item.nickname == null? '暂无信息': item.nickname}}</view>
						<view class="address">{{item.company_name}}</view>
					</view>
				</view>
				<view class="follow" v-if="item.follow == 2" @click="getFollow(item.id)">关注
				</view>
				<view class="isFollow" v-if="item.follow == 1">已关注</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				dataObj: {
					list: []
				}
			}
		},
		onLoad() {
			this.page = 1
			this.dataObj.list = [];
			this.getData();
		},
		onReachBottom() {
			this.page++
			this.getData();
		},
		methods: {
			// 获取数据
			getData() {
				this.$Requset({
					url: this.$Api['user']['getAllUsers'],
					data: {
						page: this.page,
						limit: 10
					},
					success: (res) => {
						// console.log(res);
						const {
							data,
							status,
							msg
						} = res;
						if (!status) {
							requestErrorTip('toast', msg);
							return;
						}
						this.dataObj['list'] = this.dataObj['list'].concat(data['datas']);
					}
				})
			},
			// 取消/关注
			cancelFollow(id) {
				this.$Requset({
					url: this.$Api['user']['follow'],
					data: {
						user_id: id
					},
					success: (res) => {
						// console.log(res);
						const {
							data,
							status,
							msg
						} = res;
						if (!status) {
							requestErrorTip('toast', msg);
							return;
						}
						this.page = 1
						this.dataObj.list = []
						this.getData();
					}
				})
			},
			// 关注
			getFollow(id) {
				let that = this;
				uni.showModal({
					title: '',
					content: '关注成功',
					showCancel: false,
					confirmColor: '#FE6B00',
					success: function(res) {
						if (res.confirm) {
							that.cancelFollow(id)
							// console.log('用户点击确定');
							// console.log(id)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f7f7f7;
	}

	.user {
		padding-top: 18rpx;

		.list {
			padding: 0 30rpx;
			border-radius: 22rpx;
			background-color: #FFFFFF;
			margin-bottom: 18rpx;

			.user-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1rpx solid #F3F3F3;
				padding: 28rpx 0;

				&:last-child {
					border-bottom: 0;
				}

				.head {
					display: flex;
					align-items: center;

					.head-img {
						width: 100rpx;
						height: 100rpx;
						margin-right: 20rpx;
						// background-color: #ccc;
					}

					.item {
						font-size: 24rpx;

						.item-date {
							font-size: 28rpx;
							margin-bottom: 6rpx;
						}

						.address {
							color: #7F7F7F;
						}
					}
				}

				.follow {
					color: #FFFFFF;
					padding: 10rpx 35rpx;
					font-size: 24rpx;
					border-radius: 10rpx;
					background-color: $color-common;
				}

				.isFollow {
					font-size: 24rpx;
					color: $color-common;
					padding-right: 25upx;
				}
			}
		}
	}
</style>
